var goog = goog || {};
goog.net = goog.net || {};
goog.net.cookies = goog.net.cookies || {};
goog.JsType_ = {
    UNDEFINED: "undefined"
};
goog.isDef = function (val) {
    return typeof val != goog.JsType_.UNDEFINED
};
goog.net.cookies.set = function (name, value, opt_maxAge, opt_path, opt_domain) {
    if (/;=/g.test(name)) {
        throw new Error('Invalid cookie name "' + name + '"')
    }
    if (/;/g.test(value)) {
        throw new Error('Invalid cookie value "' + value + '"')
    }
    if (!goog.isDef(opt_maxAge)) {
        opt_maxAge = -1
    }
    var domainStr = opt_domain ? ";domain=" + opt_domain : "";
    var pathStr = opt_path ? ";path=" + opt_path : "";
    var expiresStr;
    if (opt_maxAge < 0) {
        expiresStr = ""
    } else {
        if (opt_maxAge == 0) {
            var pastDate = new Date(1970, 1, 1);
            expiresStr = ";expires=" + pastDate.toUTCString()
        } else {
            var futureDate = new Date((new Date).getTime() + opt_maxAge * 1000);
            expiresStr = ";expires=" + futureDate.toUTCString()
        }
    }
    document.cookie = name + "=" + value + domainStr + pathStr + expiresStr
};
goog.net.cookies.get = function (name, opt_default) {
    var nameEq = name + "=";
    var cookie = String(document.cookie);
    for (var pos = -1;
    (pos = cookie.indexOf(nameEq, pos + 1)) >= 0;) {
        var i = pos;
        while (--i >= 0) {
            var ch = cookie.charAt(i);
            if (ch == ";") {
                i = -1;
                break
            }
        }
        if (i == -1) {
            var end = cookie.indexOf(";", pos);
            if (end < 0) {
                end = cookie.length
            }
            return cookie.substring(pos + nameEq.length, end)
        }
    }
    return opt_default
};
goog.net.cookies.remove = function (name, opt_path, opt_domain) {
    var rv = goog.net.cookies.containsKey(name);
    goog.net.cookies.set(name, "", 0, opt_path, opt_domain);
    return rv
};
goog.net.cookies.getKeyValues_ = function () {
    var cookie = String(document.cookie);
    var parts = cookie.split(/\s*;\s*/);
    var keys = [],
        values = [],
        index, part;
    for (var i = 0; part = parts[i]; i++) {
        index = part.indexOf("=");
        if (index == -1) {
            keys.push("");
            values.push(part)
        } else {
            keys.push(part.substring(0, index));
            values.push(part.substring(index + 1))
        }
    }
    return {
        keys: keys,
        values: values
    }
};
goog.net.cookies.getKeys = function () {
    return goog.net.cookies.getKeyValues_().keys
};
goog.net.cookies.getValues = function () {
    return goog.net.cookies.getKeyValues_().values
};
goog.net.cookies.isEmpty = function () {
    return document.cookie == ""
};
goog.net.cookies.getCount = function () {
    var cookie = String(document.cookie);
    if (cookie == "") {
        return 0
    }
    var parts = cookie.split(/\s*;\s*/);
    return parts.length
};
goog.net.cookies.containsKey = function (key) {
    var sentinel = {};
    return goog.net.cookies.get(key, sentinel) !== sentinel
};
goog.net.cookies.containsValue = function (value) {
    var values = goog.net.cookies.getKeyValues_().values;
    for (var i = 0; i < values.length; i++) {
        if (values[i] == value) {
            return true
        }
    }
    return false
};
goog.net.cookies.clear = function () {
    var keys = goog.net.cookies.getKeyValues_().keys;
    for (var i = keys.length - 1; i >= 0; i--) {
        goog.net.cookies.remove(keys[i])
    }
};
goog.net.cookies.MAX_COOKIE_LENGTH = 3950;
gadgets.callAsyncAndJoin = function (functions, continuation, opt_this) {
    var pending = functions.length;
    var results = [];
    for (var i = 0; i < functions.length; i++) {
        var wrapper = function (index) {
                functions[index].call(opt_this, function (result) {
                    results[index] = result;
                    if (--pending == 0) {
                        continuation(results)
                    }
                })
            };
        wrapper(i)
    }
};
gadgets.IfrGadgetService.prototype.setTitle = function (title) {
    jQuery("#" + this.f + "-chrome .dashboard-item-title").text(title)
};
gadgets.IfrGadgetService.prototype.setHeight = function (height) {
    if (height > gadgets.container.maxheight_) {
        height = gadgets.container.maxheight_
    }
    var element = document.getElementById(this.f);
    if (element) {
        element.style.height = height + "px"
    }
    AG.Cookie.save(this.f + "-fh", height)
};
gadgets.IfrGadgetService.prototype.setUserPref = function (editToken, name, value) {
    var nameValues = Array.prototype.slice.call(arguments, 1);
    AJS.$("#" + this.f).trigger("setUserPref", nameValues)
};
gadgets.CookieBasedUserPrefStore = function () {
    gadgets.UserPrefStore.call(this)
};
gadgets.CookieBasedUserPrefStore.inherits(gadgets.UserPrefStore);
gadgets.CookieBasedUserPrefStore.prototype.USER_PREFS_PREFIX = "gadgetUserPrefs-";
gadgets.CookieBasedUserPrefStore.prototype.getPrefs = function (gadget) {
    var userPrefs = {};
    var cookieName = this.USER_PREFS_PREFIX + gadget.id;
    var cookie = goog.net.cookies.get(cookieName);
    if (cookie) {
        var pairs = cookie.split("&");
        for (var i = 0; i < pairs.length; i++) {
            var nameValue = pairs[i].split("=");
            var name = decodeURIComponent(nameValue[0]);
            var value = decodeURIComponent(nameValue[1]);
            userPrefs[name] = value
        }
    }
    return userPrefs
};
gadgets.CookieBasedUserPrefStore.prototype.savePrefs = function (gadget) {
    var pairs = [];
    for (var name in gadget.getUserPrefs()) {
        var value = gadget.getUserPref(name);
        var pair = encodeURIComponent(name) + "=" + encodeURIComponent(value);
        pairs.push(pair)
    }
    var cookieName = this.USER_PREFS_PREFIX + gadget.id;
    var cookieValue = pairs.join("&");
    goog.net.cookies.set(cookieName, cookieValue)
};
gadgets.Container.prototype.userPrefStore = new gadgets.CookieBasedUserPrefStore();